#导入岭回归模型、糖尿病数据集及划分样本的方法
import numpy as np
from sklearn.linear_model import Lasso
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

#加载数据集并分割为训练集和测试集
ds = load_diabetes()
x,y =ds.data, ds.target
x_train, x_test, y_train, y_test = train_test_split(x,y,random_state=8)

#初始化并训练模型
model = Lasso()
model.fit(x_train,y_train)
attr_cot =np.sum(model.coef_ !=0)

#评估模型，计算准确率
r21 = model.score(x_train,y_train)
r22 = model.score(x_test,y_test)

#评估结果
print(f"模型在训练集上的预测准确率；{r21}")
print(f"模型在测试集上的预测准确率；{r22}")
print(f"套索回归使用的特征个数；{attr_cot}")